<?php
/*
	This SQL query will create the table to store your object.

	CREATE TABLE `gotprorole` (
	`gotproroleid` int(11) NOT NULL auto_increment,
	`contactid` int(11) NOT NULL,
	`proroleid` int(11) NOT NULL,
	`companyid` int(11) NOT NULL,
	`from` DATE NOT NULL,
	`to` DATE NOT NULL, INDEX(`contactid`,`proroleid`,`companyid`), PRIMARY KEY  (`gotproroleid`)) ENGINE=MyISAM;
*/

/**
* <b>GotProRole</b> class with integrated CRUD methods.
* @author Php Object Generator
* @version POG 3.0f / PHP5.1 MYSQL
* @see http://www.phpobjectgenerator.com/plog/tutorials/45/pdo-mysql
* @copyright Free for personal & commercial use. (Offered under the BSD license)
* @link http://www.phpobjectgenerator.com/?language=php5.1&wrapper=pdo&pdoDriver=mysql&objectName=GotProRole&attributeList=array+%28%0A++0+%3D%3E+%27Contact%27%2C%0A++1+%3D%3E+%27ProRole%27%2C%0A++2+%3D%3E+%27Company%27%2C%0A++3+%3D%3E+%27from%27%2C%0A++4+%3D%3E+%27to%27%2C%0A++5+%3D%3E+%27Document%27%2C%0A%29&typeList=array%2B%2528%250A%2B%2B0%2B%253D%253E%2B%2527BELONGSTO%2527%252C%250A%2B%2B1%2B%253D%253E%2B%2527BELONGSTO%2527%252C%250A%2B%2B2%2B%253D%253E%2B%2527BELONGSTO%2527%252C%250A%2B%2B3%2B%253D%253E%2B%2527DATE%2527%252C%250A%2B%2B4%2B%253D%253E%2B%2527DATE%2527%252C%250A%2B%2B5%2B%253D%253E%2B%2527JOIN%2527%252C%250A%2529
*/
include_once('class.pog_base.php');
include_once('class.documentgotprorolemap.php');
class GotProRole extends POG_Base
{
	public $gotproroleId = '';

	/**
	 * @var INT(11)
	 */
	public $contactId;
	
	/**
	 * @var INT(11)
	 */
	public $proroleId;
	
	/**
	 * @var INT(11)
	 */
	public $companyId;
	
	/**
	 * @var DATE
	 */
	public $from;
	
	/**
	 * @var DATE
	 */
	public $to;
	
	/**
	 * @var private array of Document objects
	 */
	private $_documentList = array();
	
	public $pog_attribute_type = array(
		"gotproroleId" => array('db_attributes' => array("NUMERIC", "INT")),
		"Contact" => array('db_attributes' => array("OBJECT", "BELONGSTO")),
		"ProRole" => array('db_attributes' => array("OBJECT", "BELONGSTO")),
		"Company" => array('db_attributes' => array("OBJECT", "BELONGSTO")),
		"from" => array('db_attributes' => array("NUMERIC", "DATE")),
		"to" => array('db_attributes' => array("NUMERIC", "DATE")),
		"Document" => array('db_attributes' => array("OBJECT", "JOIN")),
		);
	public $pog_query;
	
	
	/**
	* Getter for some private attributes
	* @return mixed $attribute
	*/
	public function __get($attribute)
	{
		if (isset($this->{"_".$attribute}))
		{
			return $this->{"_".$attribute};
		}
		else
		{
			return false;
		}
	}
	
	function GotProRole($from='', $to='')
	{
		$this->from = $from;
		$this->to = $to;
		$this->_documentList = array();
	}
	
	
	/**
	* Gets object from database
	* @param integer $gotproroleId 
	* @return object $GotProRole
	*/
	function Get($gotproroleId)
	{
		$connection = Database::Connect();
		$this->pog_query = "select * from `gotprorole` where `gotproroleid`='".intval($gotproroleId)."' LIMIT 1";
		$cursor = Database::Reader($this->pog_query, $connection);
		while ($row = Database::Read($cursor))
		{
			$this->gotproroleId = $row['gotproroleid'];
			$this->contactId = $row['contactid'];
			$this->proroleId = $row['proroleid'];
			$this->companyId = $row['companyid'];
			$this->from = $row['from'];
			$this->to = $row['to'];
		}
		return $this;
	}
	
	
	/**
	* Returns a sorted array of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array $gotproroleList
	*/
	function GetList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$connection = Database::Connect();
		$sqlLimit = ($limit != '' ? "LIMIT $limit" : '');
		$this->pog_query = "select * from `gotprorole` ";
		$gotproroleList = Array();
		if (sizeof($fcv_array) > 0)
		{
			$this->pog_query .= " where ";
			for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
			{
				if (sizeof($fcv_array[$i]) == 1)
				{
					$this->pog_query .= " ".$fcv_array[$i][0]." ";
					continue;
				}
				else
				{
					if ($i > 0 && sizeof($fcv_array[$i-1]) != 1)
					{
						$this->pog_query .= " AND ";
					}
					if (isset($this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
					{
						if ($GLOBALS['configuration']['db_encoding'] == 1)
						{
							$value = POG_Base::IsColumn($fcv_array[$i][2]) ? "BASE64_DECODE(".$fcv_array[$i][2].")" : "'".$fcv_array[$i][2]."'";
							$this->pog_query .= "BASE64_DECODE(`".$fcv_array[$i][0]."`) ".$fcv_array[$i][1]." ".$value;
						}
						else
						{
							$value =  POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$this->Escape($fcv_array[$i][2])."'";
							$this->pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
						}
					}
					else
					{
						$value = POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$fcv_array[$i][2]."'";
						$this->pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
					}
				}
			}
		}
		if ($sortBy != '')
		{
			if (isset($this->pog_attribute_type[$sortBy]['db_attributes']) && $this->pog_attribute_type[$sortBy]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$sortBy]['db_attributes'][0] != 'SET')
			{
				if ($GLOBALS['configuration']['db_encoding'] == 1)
				{
					$sortBy = "BASE64_DECODE($sortBy) ";
				}
				else
				{
					$sortBy = "$sortBy ";
				}
			}
			else
			{
				$sortBy = "$sortBy ";
			}
		}
		else
		{
			$sortBy = "gotproroleid";
		}
		$this->pog_query .= " order by ".$sortBy." ".($ascending ? "asc" : "desc")." $sqlLimit";
		$thisObjectName = get_class($this);
		$cursor = Database::Reader($this->pog_query, $connection);
		while ($row = Database::Read($cursor))
		{
			$gotprorole = new $thisObjectName();
			$gotprorole->gotproroleId = $row['gotproroleid'];
			$gotprorole->contactId = $row['contactid'];
			$gotprorole->proroleId = $row['proroleid'];
			$gotprorole->companyId = $row['companyid'];
			$gotprorole->from = $row['from'];
			$gotprorole->to = $row['to'];
			$gotproroleList[] = $gotprorole;
		}
		return $gotproroleList;
	}
	
	
	/**
	* Saves the object to the database
	* @return integer $gotproroleId
	*/
	function Save($deep = true)
	{
		$connection = Database::Connect();
		$this->pog_query = "select `gotproroleid` from `gotprorole` where `gotproroleid`='".$this->gotproroleId."' LIMIT 1";
		$rows = Database::Query($this->pog_query, $connection);
		if ($rows > 0)
		{
			$this->pog_query = "update `gotprorole` set 
			`contactid`='".$this->contactId."', 
			`proroleid`='".$this->proroleId."', 
			`companyid`='".$this->companyId."', 
			`from`='".$this->from."', 
			`to`='".$this->to."'where `gotproroleid`='".$this->gotproroleId."'";
		}
		else
		{
			$this->pog_query = "insert into `gotprorole` (`contactid`, `proroleid`, `companyid`, `from`, `to`) values (
			'".$this->contactId."', 
			'".$this->proroleId."', 
			'".$this->companyId."', 
			'".$this->from."', 
			'".$this->to."')";
		}
		$insertId = Database::InsertOrUpdate($this->pog_query, $connection);
		if ($this->gotproroleId == "")
		{
			$this->gotproroleId = $insertId;
		}
		if ($deep)
		{
			foreach ($this->_documentList as $document)
			{
				$document->Save();
				$map = new DocumentGotProRoleMap();
				$map->AddMapping($this, $document);
			}
		}
		return $this->gotproroleId;
	}
	
	
	/**
	* Clones the object and saves it to the database
	* @return integer $gotproroleId
	*/
	function SaveNew($deep = false)
	{
		$this->gotproroleId = '';
		return $this->Save($deep);
	}
	
	
	/**
	* Deletes the object from the database
	* @return boolean
	*/
	function Delete($deep = false, $across = false)
	{
		if ($across)
		{
			$documentList = $this->GetDocumentList();
			$map = new DocumentGotProRoleMap();
			$map->RemoveMapping($this);
			foreach ($documentList as $document)
			{
				$document->Delete($deep, $across);
			}
		}
		else
		{
			$map = new DocumentGotProRoleMap();
			$map->RemoveMapping($this);
		}
		$connection = Database::Connect();
		$this->pog_query = "delete from `gotprorole` where `gotproroleid`='".$this->gotproroleId."'";
		return Database::NonQuery($this->pog_query, $connection);
	}
	
	
	/**
	* Deletes a list of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param bool $deep 
	* @return 
	*/
	function DeleteList($fcv_array, $deep = false, $across = false)
	{
		if (sizeof($fcv_array) > 0)
		{
			if ($deep || $across)
			{
				$objectList = $this->GetList($fcv_array);
				foreach ($objectList as $object)
				{
					$object->Delete($deep, $across);
				}
			}
			else
			{
				$connection = Database::Connect();
				$pog_query = "delete from `gotprorole` where ";
				for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
				{
					if (sizeof($fcv_array[$i]) == 1)
					{
						$pog_query .= " ".$fcv_array[$i][0]." ";
						continue;
					}
					else
					{
						if ($i > 0 && sizeof($fcv_array[$i-1]) !== 1)
						{
							$pog_query .= " AND ";
						}
						if (isset($this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
						{
							$pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." '".$this->Escape($fcv_array[$i][2])."'";
						}
						else
						{
							$pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." '".$fcv_array[$i][2]."'";
						}
					}
				}
				return Database::NonQuery($pog_query, $connection);
			}
		}
	}
	
	
	/**
	* Associates the Contact object to this one
	* @return boolean
	*/
	function GetContact()
	{
		$contact = new Contact();
		return $contact->Get($this->contactId);
	}
	
	
	/**
	* Associates the Contact object to this one
	* @return 
	*/
	function SetContact(&$contact)
	{
		$this->contactId = $contact->contactId;
	}
	
	
	/**
	* Associates the ProRole object to this one
	* @return boolean
	*/
	function GetProrole()
	{
		$prorole = new ProRole();
		return $prorole->Get($this->proroleId);
	}
	
	
	/**
	* Associates the ProRole object to this one
	* @return 
	*/
	function SetProrole(&$prorole)
	{
		$this->proroleId = $prorole->proroleId;
	}
	
	
	/**
	* Associates the Company object to this one
	* @return boolean
	*/
	function GetCompany()
	{
		$company = new Company();
		return $company->Get($this->companyId);
	}
	
	
	/**
	* Associates the Company object to this one
	* @return 
	*/
	function SetCompany(&$company)
	{
		$this->companyId = $company->companyId;
	}
	
	
	/**
	* Creates mappings between this and all objects in the Document List array. Any existing mapping will become orphan(s)
	* @return null
	*/
	function SetDocumentList(&$documentList)
	{
		$map = new DocumentGotProRoleMap();
		$map->RemoveMapping($this);
		$this->_documentList = $documentList;
	}
	
	
	/**
	* Returns a sorted array of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array $gotproroleList
	*/
	function GetDocumentList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$sqlLimit = ($limit != '' ? "LIMIT $limit" : '');
		$connection = Database::Connect();
		$document = new Document();
		$documentList = Array();
		$this->pog_query = "select distinct * from `document` a INNER JOIN `documentgotprorolemap` m ON m.documentid = a.documentid where m.gotproroleid = '$this->gotproroleId' ";
		if (sizeof($fcv_array) > 0)
		{
			$this->pog_query .= " AND ";
			for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
			{
				if (sizeof($fcv_array[$i]) == 1)
				{
					$this->pog_query .= " ".$fcv_array[$i][0]." ";
					continue;
				}
				else
				{
					if ($i > 0 && sizeof($fcv_array[$i-1]) != 1)
					{
						$this->pog_query .= " AND ";
					}
					if (isset($document->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $document->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $document->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
					{
						if ($GLOBALS['configuration']['db_encoding'] == 1)
						{
							$value = POG_Base::IsColumn($fcv_array[$i][2]) ? "BASE64_DECODE(".$fcv_array[$i][2].")" : "'".$fcv_array[$i][2]."'";
							$this->pog_query .= "BASE64_DECODE(`".$fcv_array[$i][0]."`) ".$fcv_array[$i][1]." ".$value;
						}
						else
						{
							$value =  POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$this->Escape($fcv_array[$i][2])."'";
							$this->pog_query .= "a.`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
						}
					}
					else
					{
						$value = POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$fcv_array[$i][2]."'";
						$this->pog_query .= "a.`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
					}
				}
			}
		}
		if ($sortBy != '')
		{
			if (isset($document->pog_attribute_type[$sortBy]['db_attributes']) && $document->pog_attribute_type[$sortBy]['db_attributes'][0] != 'NUMERIC' && $document->pog_attribute_type[$sortBy]['db_attributes'][0] != 'SET')
			{
				if ($GLOBALS['configuration']['db_encoding'] == 1)
				{
					$sortBy = "BASE64_DECODE(a.$sortBy) ";
				}
				else
				{
					$sortBy = "a.$sortBy ";
				}
			}
			else
			{
				$sortBy = "a.$sortBy ";
			}
		}
		else
		{
			$sortBy = "a.documentid";
		}
		$this->pog_query .= " order by ".$sortBy." ".($ascending ? "asc" : "desc")." $sqlLimit";
		$cursor = Database::Reader($this->pog_query, $connection);
		while($rows = Database::Read($cursor))
		{
			$document = new Document();
			foreach ($document->pog_attribute_type as $attribute_name => $attrubute_type)
			{
				if ($attrubute_type['db_attributes'][1] != "HASMANY" && $attrubute_type['db_attributes'][1] != "JOIN")
				{
					if ($attrubute_type['db_attributes'][1] == "BELONGSTO")
					{
						$document->{strtolower($attribute_name).'Id'} = $rows[strtolower($attribute_name).'id'];
						continue;
					}
					$document->{$attribute_name} = $this->Unescape($rows[strtolower($attribute_name)]);
				}
			}
			$documentList[] = $document;
		}
		return $documentList;
	}
	
	
	/**
	* Associates the Document object to this one
	* @return 
	*/
	function AddDocument(&$document)
	{
		if ($document instanceof Document)
		{
			if (in_array($this, $document->gotproroleList, true))
			{
				return false;
			}
			else
			{
				$found = false;
				foreach ($this->_documentList as $document2)
				{
					if ($document->documentId > 0 && $document->documentId == $document2->documentId)
					{
						$found = true;
						break;
					}
				}
				if (!$found)
				{
					$this->_documentList[] = $document;
				}
			}
		}
	}
}
?>